package com.huayun.weexutil;


import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.huayun.manager.AppManager;
import com.huayun.util.utils.SharedPreferenceUtil;
import com.huayun.util.utils.ToastUtil;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;

import com.huayun.util.SchemeUtil;
import com.huayun.util.utils.LogUtil;

/**
 * Description: module
 * Created on 2019/8/2 0002 10:28:49
 * author:AHuangSHang
 */

public class WXEventModule extends WXModule {

    /**
     * Log
     *
     * @param message log Message
     */
    @JSMethod(uiThread = true)
    public void Log(String message) {
        LogUtil.e("huaYun log ------------>" + message);
    }

    /**
     * get assets file
     *
     * @param name     the name of file
     * @param type     file suffix
     * @param callback callback
     */
    @JSMethod(uiThread = true)
    public void getFilePath(String name, String type, JSCallback callback) {
        if (TextUtils.isEmpty(type)) {
            callback.invokeAndKeepAlive("file:///android_asset/" + name);
        } else {
            callback.invokeAndKeepAlive("file:///android_asset/" + name + "." + type);
        }
    }

    /**
     * Open any page,weexPage and nativePage
     *
     * @param uri a designated uri
     */
    @JSMethod(uiThread = true)
    @JavascriptInterface
    public void openAnyView(String uri) {
        openAnyViewFinish(uri, false);
    }

    @JSMethod(uiThread = true)
    @JavascriptInterface
    public void openAnyViewFinish(String uri, boolean finish) {
        if (!TextUtils.isEmpty(uri)) {
            SchemeUtil.getInstance().jump(uri, finish);
        } else {
            ToastUtil.showMessage("打开页面链接不能为空");
        }
    }

    /**
     * Open weexPage
     *
     * @param uri a designated uri
     */
    @JSMethod(uiThread = true)
    @JavascriptInterface
    public void openView(String uri) {
        openViewFinish(uri, false);
    }

    /**
     * Open weexPage
     *
     * @param uri a designated uri
     */
    @JSMethod(uiThread = true)
    @JavascriptInterface
    public void openViewFinish(String uri, boolean finish) {
        if (!TextUtils.isEmpty(uri)) {
            uri = "name=" + WeexActivity.class.getName() + "&" + uri;
            openAnyViewFinish(uri, finish);
        } else {
            ToastUtil.showMessage("打开页面链接不能为空");
        }
    }

    /**
     * if use router ,you can use this method open a new page
     *
     * @param path routerPath
     */
    @JSMethod(uiThread = true)
    @JavascriptInterface
    public void saveRouterPath(String path) {
        SharedPreferenceUtil.put("routerPath", path);
        if (!TextUtils.isEmpty(path) && path.equals("/")) {
            AppManager.getAppManager().finishActivityWithClassName(WeexActivity.class);
        }
    }
}
